package buzzword.gui;

import java.awt.HeadlessException;

import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import json.JSONException;

import buzzword.Client;
import buzzword.GameEndedException;
import buzzword.GameNotStartetException;
import buzzword.ServerCommunicationException;

public class GameControlPanel extends JPanel{

	private JButton buttonbBingo;
	private JList listDrawnWords;
	private boolean win = false;
	
	public GameControlPanel(int height) {
		setSize(100, height);
		setLayout(null);
		this.setOpaque(false);
		buttonbBingo = new JButton("Bingo");
		buttonbBingo.setBounds(0,0,100,80);
		buttonbBingo.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				try {
					if(GuiMainFrame.getClient().bingo()) {
						win = true;
						JOptionPane.showMessageDialog(((GuiMainFrame)SwingUtilities.getRoot(GameControlPanel.this)), "You win.");
						((GuiMainFrame)SwingUtilities.getRoot(GameControlPanel.this)).switchActionPanel(new MainMenuPanel());
					}else {
						JOptionPane.showMessageDialog(((GuiMainFrame)SwingUtilities.getRoot(GameControlPanel.this)), "You have no Bingo.");
						((GuiMainFrame)SwingUtilities.getRoot(GameControlPanel.this)).switchActionPanel(new MainMenuPanel());
					}
				} catch (HeadlessException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (JSONException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
			}
		});
		this.add(buttonbBingo);
		listDrawnWords = new JList();
		listDrawnWords.setBounds(0,100,100,this.getHeight());
		this.add(listDrawnWords);
		getWords.start();
	}
	
	private boolean isRunning=false;
	private Thread getWords = new Thread(new Runnable() {
		
		@Override
		public void run() {
			isRunning = true;
			while(isRunning) {
				try {
					Object[] tempList = GuiMainFrame.getClient().getDrawnWords().toArray();
					if(tempList.length > 3) {
						Object[] smallTempList = new Object[3];
						smallTempList[0] = tempList[tempList.length-3];
						smallTempList[1] = tempList[tempList.length-2];
						smallTempList[2] = tempList[tempList.length-1];
						listDrawnWords.setListData(smallTempList);
					} else {
						listDrawnWords.setListData(tempList);

					}
					Thread.sleep(500);
				} catch (JSONException e) {
					isRunning=false;
					e.printStackTrace();
				} catch (ServerCommunicationException e) {
					isRunning=false;
					e.printStackTrace();
				} catch (GameNotStartetException e) {
					isRunning=false;
					e.printStackTrace();
				} catch (InterruptedException e) {
					isRunning=false;
					e.printStackTrace();
				} catch (GameEndedException e) {
					if(!win) {
						JOptionPane.showMessageDialog(((GuiMainFrame)SwingUtilities.getRoot(GameControlPanel.this)), "An other Player has a Bingo.");
						((GuiMainFrame)SwingUtilities.getRoot(GameControlPanel.this)).switchActionPanel(new MainMenuPanel());
					}
					isRunning =false;
				}
			}
			
		}
	});
}
